﻿using System;
using System.Web.UI;
using SBACN.WebSite.Cache;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;
using System.Linq.Expressions;

namespace SBACN.WebSite.Admin
{
	public partial class NewsEdit : AdminBasePage
	{
		/// <summary>
		/// 查看新闻的系统字典类型
		/// </summary>
		public string SystemType
		{
			get
			{
				//if (ViewState["SystemType"] == null)
				//    return "00020001";//要闻导读，默认值
				//else
				//    return ViewState["SystemType"].ToString();
				return this.SystemCode;
			}
			private set
			{
				ViewState["SystemType"] = this.SystemCode;
			}
		}

		/// <summary>
		/// 是否是扩展编辑
		/// </summary>
		public bool ExEdit
		{
			get
			{
				if (ViewState["EditType"] == null)
				{
					return true;
				}
				else
				{
					return Convert.ToBoolean(ViewState["EditType"]);
				}
			}
			private set
			{
				ViewState["EditType"] = value;
			}
		}

		/// <summary>
		/// 是否发布到英文站点
		/// </summary>
		public bool IsEN
		{
			get
			{
				if (ViewState["IsEN"] == null)
				{
					return false;
				}
				else
				{
					return Convert.ToBoolean(ViewState["IsEN"]);
				}
			}
			private set
			{
				ViewState["IsEN"] = value;
			}
		}

		public int NewsID
		{
			get
			{
				if (ViewState["NewsID"] == null)
				{
					return -1;
				}
				else
				{
					return Convert.ToInt32(ViewState["NewsID"]);
				}
			}
			private set
			{
				ViewState["NewsID"] = value;
			}
		}

		/// <summary>
		/// 是否为专题活动
		/// </summary>
		public bool IsSpa
		{
			get
			{
				if (ViewState["IsSpa"] == null)
				{
					return false;
				}
				else
				{
					return Convert.ToBoolean(ViewState["IsSpa"]);
				}
			}
			private set
			{
				ViewState["IsSpa"] = value;
			}
		}
		/// <summary>
		/// 是否为新增
		/// </summary>
		public bool IsAdd
		{
			get
			{
				if (ViewState["IsAdd"] == null)
				{
					return false;
				}
				else
				{
					return Convert.ToBoolean(ViewState["IsAdd"]);
				}
			}
			private set
			{
				ViewState["IsAdd"] = value;
			}
		}

		public string ImageSize
		{
			get
			{
				if(IsSpa)
				{
					return "75 X 70";
				}
				else
				{
					return "756 X 219";
				}
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				if(!string.IsNullOrWhiteSpace(Request["st"]))
				{
					this.SystemType = Request["st"].Trim();
				}

				if (!string.IsNullOrWhiteSpace(Request["ex"]))
				{
					this.ExEdit = Request["ex"].Trim() == "1";
				}
				else
				{
					this.ExEdit = false;
				}

				if (!string.IsNullOrWhiteSpace(Request["en"]))
				{
					this.IsEN = Request["en"].Trim() == "1";
				}
				else
				{
					this.IsEN = false;
				}

				if (!string.IsNullOrWhiteSpace(Request["x"]))
				{
					this.IsSpa = Request["x"].Trim() == "1";
				}
				else
				{
					this.IsSpa = false;
				}

				if (!string.IsNullOrWhiteSpace(Request["ad"]))
				{
					this.IsAdd = Request["ad"].Trim() == "1";
				}
				else
				{
					this.IsAdd = false;
				}

				int newsID = 0;
				if (!string.IsNullOrWhiteSpace(Request["id"]) && int.TryParse(Request["id"], out newsID))
				{
					this.NewsID = newsID;
				}

				InitCtrl();
			}
		}

		private void InitCtrl()
		{
			if (IsSpa)
			{
				this.chb_IsAutoScroll.Visible = false;
				this.chb_IsPrivate.Visible = false;
				this.chb_IsImageNews.Visible = true;
				this.chb_IsImageNews.Text = "专题活动";
				this.chb_IsImageNews.Checked = true;
				this.chb_IsImageNews.Enabled = false;
			}
			else
			{
				this.chb_IsAutoScroll.Visible = true;
				this.chb_IsPrivate.Visible = true;
				this.chb_IsImageNews.Visible = true;
				this.chb_IsImageNews.Text = "发布到图片新闻";
				this.chb_IsImageNews.Checked = false;
				this.chb_IsImageNews.Enabled = true;
			}

			TB_SystemMap map = SystemMapCache.Instance[this.SystemType];
			this.Header.Title = "内容编辑:" + map.Name;
			if (NewsID > 0)//edit
			{
				TB_News news = DAL.DALHelper.Instance.GetObject<TB_News>("ID", NewsID);
				RenderPage(news);
			}
			else//create
			{
				this.txt_CreateDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
				if(!IsAdd)
				{
					Expression<Func<TB_News, bool>> f = (p => p.Type == SystemType && p.IsEn == IsEN);
					TB_News news = DAL.DALHelper.Instance.GetFirstOrDefaultObject(f);
					RenderPage(news);
				}
			}


			if(SystemCode == "00040002")
			{
				this.chb_IsPrivate.Visible = true;
				this.chb_IsPrivate.Checked = true;
				//this.chb_IsPrivate.Enabled = false;
			}
		}

		private void RenderPage(TB_News news)
		{
			if (news != null)
			{
				this.txt_Title.Text = news.Title;
				this.txt_CreateDate.Text = news.CreateDate.ToString("yyyy-MM-dd");
				this.txt_Summary.Text = news.Summary;
				this.txt_Content.Text = news.Content;
				this.chb_IsAutoScroll.Checked = news.IsAutoScorllNews;
				this.chb_IsImageNews.Checked = news.IsImageNews;
				this.chb_IsPrivate.Checked = news.IsPrivate;
				this.linkMainPic.HRef = news.MainImageUrl;
				this.linkMainPic.Visible = !String.IsNullOrWhiteSpace(news.MainImageUrl);
				this.linkAttarchment.HRef = news.AttachmentsUrl;
				this.linkAttarchment.Visible = !String.IsNullOrWhiteSpace(news.AttachmentsUrl);
				this.NewsID = news.ID;
			}
		}

		protected void btn_Save_Click(object sender, EventArgs e)
		{
			if(string.IsNullOrWhiteSpace(txt_Title.Text))
			{
				this.lbl_Message.Text = "标题不能为空";
				return;
			}
			if(string.IsNullOrWhiteSpace(txt_CreateDate.Text))
			{
				this.lbl_Message.Text = "发布时间不能为空";
				return;
			}
			DateTime dtCreate = DateTime.Now;
			if(!DateTime.TryParse(this.txt_CreateDate.Text,out dtCreate))
			{
				this.lbl_Message.Text = "发布时间格式不正确";
				return;
			}
			if (this.chb_IsImageNews.Checked && string.IsNullOrWhiteSpace(file_Image.FileName) && string.IsNullOrWhiteSpace(linkMainPic.HRef))
			{
				this.lbl_Message.Text = "图片新闻必须上传新闻图片";
				return;
			}

			if (NewsID > 0)//Edit
			{
				TB_News news = DAL.DALHelper.Instance.GetObject<TB_News>("ID", NewsID);
				if(news != null)
				{
					news.Title = this.txt_Title.Text.Trim();
					news.Summary = this.txt_Summary.Text.Trim();
					news.Content = this.txt_Content.Text;
					news.IsPrivate = this.chb_IsPrivate.Checked;
					news.IsAutoScorllNews = this.chb_IsAutoScroll.Checked;
					news.IsImageNews = this.chb_IsImageNews.Checked;
					news.CreateDate = dtCreate;
					news.CreateBy = AdminInfo.UserName;
					if(!string.IsNullOrWhiteSpace(this.file_Image.FileName))
					{
						news.MainImageUrl = BasePage.UploadFile(this.SystemType,this.file_Image.FileName,this.file_Image.FileBytes);
						TB_UploadInfo upload = DAL.DALHelper.Instance.Create<TB_UploadInfo>();
						upload.CreateBy = AdminInfo.UserName;
						upload.CreateDate = DateTime.Now;
						upload.FilePath = news.MainImageUrl;
						upload.FileType = BasePage.GetFileExt(this.file_Image.FileName);
						upload.Name = "图片新闻主图片";
						upload.PrimaryID = news.ID;
						upload.Type = this.SystemType;
						DAL.DALHelper.Instance.Add<TB_UploadInfo>(upload);
					}
					if (!string.IsNullOrWhiteSpace(this.file_Attarchment.FileName))
					{
						news.AttachmentsUrl = BasePage.UploadFile(this.SystemType, this.file_Attarchment.FileName, this.file_Attarchment.FileBytes);
						TB_UploadInfo upload = DAL.DALHelper.Instance.Create<TB_UploadInfo>();
						upload.CreateBy = AdminInfo.UserName;
						upload.CreateDate = DateTime.Now;
						upload.FilePath = news.AttachmentsUrl;
						upload.FileType = BasePage.GetFileExt(this.file_Attarchment.FileName);
						upload.Name = "附件";
						upload.PrimaryID = news.ID;
						upload.Type = this.SystemType;
						DAL.DALHelper.Instance.Add<TB_UploadInfo>(upload);
					}
				}
				DAL.DALHelper.Instance.UpdateWithTransaction<TB_News>(news, "ID");
				this.linkMainPic.HRef = news.MainImageUrl;
				this.linkMainPic.Visible = !String.IsNullOrWhiteSpace(news.MainImageUrl);
				this.linkAttarchment.HRef = news.AttachmentsUrl;
				this.linkAttarchment.Visible = !String.IsNullOrWhiteSpace(news.AttachmentsUrl);
			}
			else//Create
			{
				TB_News news = DAL.DALHelper.Instance.Create<TB_News>();
				news.Title = this.txt_Title.Text.Trim();
				news.Summary = this.txt_Summary.Text.Trim();
				news.CreateDate = dtCreate;
				news.Content = this.txt_Content.Text;
				news.CreateBy = AdminInfo.UserName;
				news.IsPrivate = this.chb_IsPrivate.Checked;
				news.IsAutoScorllNews = this.chb_IsAutoScroll.Checked;
				news.IsImageNews = this.chb_IsImageNews.Checked;
				news.Type = this.SystemType;
				news.IsEn = this.IsEN;//只有新增时才设置当前新闻的中、英文类型，不可修改
				if (!string.IsNullOrWhiteSpace(this.file_Image.FileName))
				{
					news.MainImageUrl = BasePage.UploadFile(this.SystemType, this.file_Image.FileName, this.file_Image.FileBytes);
				}
				if (!string.IsNullOrWhiteSpace(this.file_Attarchment.FileName))
				{
					news.AttachmentsUrl = BasePage.UploadFile(this.SystemType, this.file_Attarchment.FileName, this.file_Attarchment.FileBytes);
				}
				DAL.DALHelper.Instance.Add<TB_News>(news);
				DAL.DALHelper.Instance.SaveChanges();
				
				if (!string.IsNullOrWhiteSpace(this.file_Image.FileName))
				{
					TB_UploadInfo upload = DAL.DALHelper.Instance.Create<TB_UploadInfo>();
					upload.CreateBy = AdminInfo.UserName;
					upload.CreateDate = DateTime.Now;
					upload.FilePath = news.MainImageUrl;
					upload.FileType = BasePage.GetFileExt(this.file_Image.FileName);
					upload.Name = "图片新闻主图片";
					upload.PrimaryID = news.ID;
					upload.Type = this.SystemType;
					DAL.DALHelper.Instance.Add<TB_UploadInfo>(upload);
				}
				if (!string.IsNullOrWhiteSpace(this.file_Attarchment.FileName))
				{
					TB_UploadInfo upload = DAL.DALHelper.Instance.Create<TB_UploadInfo>();
					upload.CreateBy = AdminInfo.UserName;
					upload.CreateDate = DateTime.Now;
					upload.FilePath = news.AttachmentsUrl;
					upload.FileType = BasePage.GetFileExt(this.file_Attarchment.FileName);
					upload.Name = "附件";
					upload.PrimaryID = news.ID;
					upload.Type = this.SystemType;
					DAL.DALHelper.Instance.Add<TB_UploadInfo>(upload);
				}
				DAL.DALHelper.Instance.SaveChanges();
				this.linkMainPic.HRef = news.MainImageUrl;
				this.linkMainPic.Visible = !String.IsNullOrWhiteSpace(news.MainImageUrl);
				this.linkAttarchment.HRef = news.AttachmentsUrl;
				this.linkAttarchment.Visible = !String.IsNullOrWhiteSpace(news.AttachmentsUrl);
			}
			RefreshNewsList();
		}

		private void RefreshNewsList()
		{
			string clientJs = "<script type=\"text/javascript\" language=\"javascript\">";
			clientJs += "if(window.opener && window.opener.RefreshNewsList) window.opener.RefreshNewsList('" + SystemCode + "');";
			clientJs += "window.close();</script>";
			Response.Write(clientJs);
			Response.End();
		}
	}
}